<template>
    <div class="container">
        <div class="left-box">
            <div class="profile-icon">
                <img src="https://codelover.club/files/stutasks/userID_874/currNo_47/FvqQzLQzCCC4GY4B03QSAid3OAgf.jpg"
                    alt="Profile Icon" width="160" height="160" />
                <span>超级学生</span>
            </div>
            <div>
                <h3 class="title">个人资料</h3>
                <el-tabs v-model="activeTab" @tab-click="handleTabClick" class="tabs">
                    <el-tab-pane label="基本资料" name="basic"></el-tab-pane>
                    <el-tab-pane label="详细资料" name="detail"></el-tab-pane>
                </el-tabs>
            </div>
            <div>
                <h3 class="title">学业展示</h3>
                <el-tabs v-model="activeTab" @tab-click="handleTabClick" class="tabs">
                    <el-tab-pane label="赛季评定" name="my"></el-tab-pane>
                    <el-tab-pane label="我的课程" name="work"></el-tab-pane>
                </el-tabs>
            </div>
            <div>
                <h3 class="title">未完持续</h3>
                <el-tabs v-model="activeTab" @tab-click="handleTabClick" class="tabs">
                    <el-tab-pane label="敬请期待" name="time"></el-tab-pane>
                </el-tabs>
            </div>
        </div>
        <div class="right-box">
            <div v-if="activeTab === 'basic'" class="basic-item">
                <span class="basic-title">密码</span>
                <div class="basic-buttons">
                    <button class="basic-btn">隐藏</button>
                    <button class="basic-btn">改变</button>
                </div>
            </div>
            <div v-if="activeTab === 'detail'" class="detail-item">
                <div class="form-group">
                    <label for="nickname" class="form-label">昵称:</label>
                    <input type="text" id="nickname" v-model="nickname" class="form-input" />
                </div>
                <div class="form-group">
                    <label for="phone" class="form-label">手机号:</label>
                    <input type="tel" id="phone" v-model="phone" class="form-input" />
                </div>
                <div class="form-group">
                    <label for="gender" class="form-label">性别:</label>
                    <select id="gender" v-model="gender" class="form-select">
                        <option value="male">男</option>
                        <option value="female">女</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="bio" class="form-label">个人简介:</label>
                    <textarea id="bio" v-model="bio" class="form-textarea"></textarea>
                </div>
                <div class="form-group">
                    <label for="tags" class="form-label">我的标签:</label>
                    <input type="text" id="tags" v-model="tags" class="form-input" />
                </div>
                <div class="save-button">
                    <button @click="saveProfile" class="btn btn-primary">保存</button>
                </div>
            </div>
            <div v-if="activeTab === 'my'" class="my">
                <el-table :data="tableData">
                    <el-table-column prop="name" label="课程名"></el-table-column>
                    <el-table-column prop="limit" label="学分"></el-table-column>
                    <el-table-column prop="credit" label="绩点"></el-table-column>
                </el-table>
            </div>
            <div v-if="activeTab === 'work'" class="work" @click="goDetail(Number(id))">
                <div v-for="(course, index) in courseData" :key="index" class="course-item">
                    <h3>{{ course.name }}</h3>
                    <p>{{ course.currDesc }}</p>
                    <img :src="course.iconPath" alt="Course Icon" width="420" height="470" />
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import useDataStore from "../stores/Data";
import { RouterLink, RouterView } from 'vue-router';

export default {
    // 使用 inject 选项接收 homeData
    inject: ['homeData'],

    // 在 data 函数中，将 homeData 分配给 tableData 和 courseData
    data() {
        return {
            activeTab: 'basic',
            nickname: '',
            phone: '',
            gender: 'male',
            bio: '',
            tags: '',
            tableData: this.homeData, // 使用 homeData 作为 tableData 的初始值
            courseData: this.homeData, // 使用 homeData 作为 courseData 的初始值
        };
    },
    created() {
        const savedProfileData = JSON.parse(localStorage.getItem('profileData'));
        if (savedProfileData) {
            this.nickname = savedProfileData.nickname;
            this.phone = savedProfileData.phone;
            this.gender = savedProfileData.gender;
            this.bio = savedProfileData.bio;
            this.tags = savedProfileData.tags;
        }
    },
    mounted() {
        const useData = useDataStore();
        useData.getHomeData().then((data) => {
            this.name = data.name;
            this.credit = data.credit;
            this.limit = data.limit;
            this.currDesc = data.currDesc;
            this.iconPath = data.iconPath;

        });
    },
    methods: {
        handleTabClick(tab) {
            this.activeTab = tab.name;
        },
        saveProfile() {
            const profileData = {
                nickname: this.nickname,
                phone: this.phone,
                gender: this.gender,
                bio: this.bio,
                tags: this.tags,
            };
            localStorage.setItem('profileData', JSON.stringify(profileData));
            console.log('保存个人资料:', profileData);
        },
    },
    // beforeRouteEnter(to, from, next) {
    //     if (to.path === '/Oindex') {
    //         next(vm => {
    //             vm.$parent.showOindex = true;
    //         });
    //     } else {
    //         next(vm => {
    //             vm.$parent.showOindex = false;
    //         });
    //     }
    // },

    // // 路由更新时
    // beforeRouteUpdate(to, from, next) {
    //     if (to.path === '/Oindex') {
    //         this.$parent.showOindex = true;
    //     } else {
    //         this.$parent.showOindex = false;
    //     }
    //     next();
    // }
};
</script>

<style scoped>
.container {
    display: flex;
    height: 100vh;
    padding: 40px;
    margin-top: 25px;
}

.left-box {
    width: 315px;
    max-width: 800px;
    background-color: #f5f5f5;
    padding: 30px;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    margin-right: 20px;
}

.profile-icon {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 30px;
}

.title {
    font-size: 22px;
    font-weight: bold;
    margin-bottom: 15px;
}

.tabs {
    border-top: 1px solid #e6e6e6;
    padding-top: 15px;
}

.right-box {
    flex: 1;
    background-color: #fff;
    padding: 12px;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    overflow: auto;
}

.basic-item,
.detail-item {
    display: flex;
    flex-direction: column;
    gap: 20px;
    /* Adds space between form groups */
}

.basic-title {
    font-weight: bold;
}

.basic-buttons {
    display: flex;
    gap: 12px;
    /* Adds space between buttons */
}

.basic-btn {
    background-color: #f5f5f5;
    border: none;
    padding: 8px 14px;
    border-radius: 6px;
    cursor: pointer;
}

.form-group {
    display: flex;
    align-items: center;
    padding: 10px;
}

.form-label {
    width: 100px;
    text-align: right;
    margin-right: 15px;
}

.form-input,
.form-select,
.form-textarea {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid #e6e6e6;
    border-radius: 6px;
}

.form-textarea {
    height: 120px;
}

.save-button {
    display: flex;
    justify-content: flex-end;
    margin-top: 25px;
}

.btn {
    background-color: #007bff;
    color: #fff;
    border: none;
    padding: 10px 18px;
    border-radius: 6px;
    cursor: pointer;
}

.btn-primary {
    background-color: #007bff;
}

.el-tabs .el-tab-pane {
    font-size: 30px;
}

/* Responsive design */
@media (min-width: 768px) {
    .container {
        width: 1350px;
        /* 设置容器宽度 */
        height: 850px;
        /* 设置容器高度 */
        margin: 0 auto;
        /* 水平居中容器 */
        margin-top: 25px;
    }

    .left-box,
    .right-box {
        height: 100%;
        /* 使左右盒子的高度充满容器 */
    }
}

@media (max-width: 768px) {
    .container {
        flex-direction: column;
        margin-top: 25px;
    }

    .left-box,
    .right-box {
        width: 100%;
        margin-right: 0;
        margin-bottom: 20px;
    }
}

.work {
    display: grid;
    /* 使用网格布局 */
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    /* 自适应列 */
    gap: 20px;
    /* 列之间的间距 */
    padding: 20px;
    background-color: #f9f9f9;
    /* 轻微的背景色，增加区分度 */
    border-radius: 8px;
    /* 圆角边框 */
}

.course-item {
    background-color: #ffffff;
    /* 卡片背景色 */
    border-radius: 8px;
    /* 卡片圆角边框 */
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    /* 卡片阴影 */
    padding: 20px;
    transition: transform 0.2s ease-in-out;
    /* 鼠标悬浮时的动画效果 */
}

.course-item:hover {
    transform: translateY(-5px);
    /* 鼠标悬浮时轻微向上移动 */
}

.course-item h3 {
    font-size: 1.5em;
    /* 标题字体大小 */
    margin-bottom: 0.5em;
    /* 标题下方间距 */
}

.course-item p {
    color: #555;
    /* 描述文本颜色 */
    font-size: 1em;
    /* 描述文本字体大小 */
    line-height: 1.6;
    /* 描述文本行高 */
    margin-bottom: 1em;
    /* 描述文本下方间距 */
}

.course-item img {
    border-radius: 8px;
    /* 图片圆角边框 */
    object-fit: cover;
    /* 保持图片的比例 */
    width: 100%;
    /* 图片宽度 */
    height: auto;
    /* 图片高度自动调整 */
    margin-bottom: 20px;
    /* 图片下方间距 */
}
</style>